<?php 
$group = array();

foreach ( $this->rsHanghoadvngay as $value ) {
    $group[$value['Ngay']][] = $value;
}

//var_dump($group);

?>

<header class="panel-heading">
	<strong> Danh Sách món theo ngày </strong>
</header>

<div class="panel-body">
	<div class="adv-table editable-table ">
		<div class="clearfix">
			<div class="btn-group">
				<p>
				<a class="btn btn-success" href="<?php echo _BASE_URL?>quantri/hanghoadvngay/them">
					Tạo Thực Đơn <i class="icon-plus"></i>
				</a>
				</p><br />
				
				<p>
				<strong>Từ ngày: </strong>
				
				<strong>Đến ngày: </strong>
				</p>
				
			</div>
			
			<div class="btn-group pull-right">
				<button data-toggle="dropdown" class="btn dropdown-toggle">Xuất dữ liệu <i class="icon-angle-down"></i></button>
				<ul class="dropdown-menu pull-right">
					<li><a href="#">Xem in</a></li>
					<li><a href="#">Xuất PDF</a></li>
					<li><a href="#">Xuất Excel</a></li>
				</ul>
			</div>
		</div>
		<div class="space15"></div>
	  <div role="grid" class="dataTables_wrapper form-inline" id="editable-sample_wrapper">
		
		<?php
		foreach($group as $gr)
		{
		?>
		
		<table id="editable-sample" class="table table-striped table-hover table-bordered dataTable" aria-describedby="editable-sample_info">
			<thead>
				<tr>
					<th class="sorting_disabled" role="columnheader" colspan="4" style="width: 50px;">Ngày: <?php echo date_format(date_create($gr[0]['Ngay']), 'd/m/Y') ?></th>
				</tr>
				<tr role="row">
					<th class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 168px;" aria-label="Username">Tên Hàng</th>
					<th class="sorting_disabled" role="columnheader" rowspan="1" colspan="1" style="width: 168px;" aria-label="Username">Số Lượng</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 90px;" aria-label="Delete: activate to sort column ascending">Trạng Thái</th>
					<th class="sorting" role="columnheader" tabindex="0" aria-controls="editable-sample" rowspan="1" colspan="1" style="width: 70px;" aria-label="Delete: activate to sort column ascending"></th>
				</tr>
		  </thead>
		  
		  <tbody role="alert" aria-live="polite" aria-relevant="all">
			  <?php 
			  foreach($gr as $rs) { 
				?>
			  <tr>		 
			   	  
				  <td class="sorting_1"><?php echo $rs['TenHanghoadv'] ?></td>	
				  <td class="sorting_1"><?php echo $rs['SoLuong'] ?></td>					 			  
				  <td style="text-align: center" class="center"> <a href=""><i class="icon-check-sign"></i></a> </td>
				  <td style="text-align: center">
				  	<a class="ograne  edit" data-toggle="modal" href="#addModal" idHanghoadvngay="<?php echo $rs['Id'] ?>"><i class="icon-edit"></i></a> | 
				  	<a class="red xoa" href="javascript:;" class="delete" data="<?php echo $rs['Id']?>" ><i class="icon-remove"></i></a></td>
			  </tr>
			  <?php } ?>
			</tbody>
		</table>
		
		
		<?php
		}
		?>
		
		
  </div>
	</div>
</div>   

<!-- Add Modal -->
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog">
	  <div class="modal-content">
		  <div class="modal-header">
			  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			  <h4 class="modal-title">Danh sách thực đơn</h4>
		  </div>
		  <div class="modal-body">
	
			  <form id="frmaddhanghoadvngay" class="form-horizontal tasi-form" method="get">
			  <input type="hidden" name="idHanghoadvngay" id="idHanghoadvngay" value="-1" />
				<div class = "form-group" id ="grpngay">
					<label class="col-sm-2 col-sm-2 control-label">Ngày</label>
					<div class="col-sm-10">
					<input name="ngay" id="ngay" type="text" class="form-control" data-mask="99/99/9999" placeholder="" />
						<span class="help-inline">ngày/tháng/năm</span>
					</div>
				</div>
				<div class = "form-group" id ="grptenhang">
					<label class="col-sm-2 col-sm-2 control-label"> Tên hàng</label>
					<div class="col-sm-10">
					<input name="IdHangHoaDV" id="IdHangHoaDV" type="text" class="form-control" value =""/>
					</div>
				</div>
				<div class = "form-group" id ="grpsoluong">
					<label class="col-sm-2 col-sm-2 control-label"> Số lượng</label>
					<div class="col-sm-10">
					<input name="soluong" id="soluong" type="text" class="form-control" value = "">
					</div>
				</div>
				<div class="form-group">
					<label class="col-sm-2 col-sm-2 control-label">Trạng thái</label>
					<div class="col-sm-10">
						<input name="trangthai" type="checkbox" checked="" data-toggle="switch" />
					</div>
				</div>
			  </form>

		  </div>
		  <div class="modal-footer">
			  <button data-dismiss="modal" class="btn btn-default" type="button">Đóng</button>
			  <button class="btn btn-success" type="button" id="luuthaydoi">Lưu thay đổi</button>
			  <button class="btn btn-success" type="button">Lưu & Thêm mới</button>
		  </div>
	  </div>
  </div>
</div>
<!-- modal -->



<!-- END JAVASCRIPTS -->

<script>
jQuery(document).ready(function() {
	  EditableTable.init();
		
		  jQuery("#luuthaydoi").click(function(){

			var values = $('#frmaddhanghoadvngay').serialize();
			
			  soluong =$('#soluong').val();
			  $idHanghoadvngay = $('#idHanghoadvngay').val();			 
			  
			  var saveUrl = "<?php echo _BASE_URL?>/quantri/hanghoadvngay/saveupdate";
			  var formData = {soluong: soluong, idHanghoadvngay: idHanghoadvngay}; //Array 

			  var oTable = $('#editable-sample').dataTable();
			  
			  $.ajax({
				  url : saveUrl,
				  type: "POST",
				  data: values,
				  success: function(data, textStatus, jqXHR)
				  {
					  alert("Đã lưu thành công!");				 
					  location.href= "<?php echo _BASE_URL?>quantri/hanghoadvngay";
					  $(".btn-default").click();
				  },
				  error: function (jqXHR, textStatus, errorThrown)
				  {
					  alert('Thất bại');
				  }
				});
		  });
		  
	  $(".xoa").click(function(){

		  if (confirm("Bạn có muốn xóa dòng này không ?") == false) {
              return;
          }
          
		var deleteUrl = "<?php echo _BASE_URL?>/quantri/hanghoadvngay/xoa";
		var idHanghoadvngay = $(this).attr('data');
		var formData = {id: idHanghoadvngay}; //Array

		var tr = $(this).closest('tr');

		
		$.ajax({
			  url : deleteUrl,
			  type: "POST",
			  data : formData,
			  success: function(data, textStatus, jqXHR)
			  {
				  alert('Xóa thành công!');
				  deleteRowofTable(tr);
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Xóa thất bại');
			  }
		});
		
	  });

	  
	  function deleteRowofTable(tr)
	  {
		tr.css("background-color","#FF3700");
			tr.fadeOut(400, function(){
			tr.remove();
		});
	  }


	  ////////////////////////////////////////////////////////////////////
	  /// Sua 
	  $(".edit").click(function(){
		  var idHanghoadvngay = $(this).attr('idHanghoadvngay');
		  var editUrl = "<?php echo _BASE_URL?>quantri/hanghoadvngay/sua/id/" + idHanghoadvngay;		
		 
		  $.ajax({
			  url : editUrl,
			  type: "POST",
			  //data : formData,
			  dataType: "text",
			  
			  success: function(data, textStatus, jqXHR)
			  {				
				 
				  var Obhanghoadvngay = $.parseJSON(data);				 
				  $("#idHanghoadvngay").val(idHanghoadvngay);
				  $('#ngay').val(Obhanghoadvngay[0].Ngay);
				  $('#IdHangHoaDV').val(Obhanghoadvngay[0].TenHanghoadv);				  
				  $('#soluong').val(Obhanghoadvngay[0].SoLuong);
				  $("#trangthai").val(Obhanghoadvngay[0].TrangThai);
				  
			  },
			  error: function (jqXHR, textStatus, errorThrown)
			  {
				  alert('Sửa thất bại');
			  }
			});
	  });	 
});
</script>				  
